﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZhuGe.DAL;
using ZhuGe.DAL.SQLite;
using ZhuGe.DAL.SqlServer;

namespace ZhuGe.DataManage
{
    public partial class frmTables : Form
    {
        public frmTables()
        {
            InitializeComponent();

            SqlHelper.SetConnect(Conn);
        }

        #region  数据库相关配置
        public string Conn = "Data Source=.;Initial Catalog=TestDB;Persist Security Info=True;User ID=sa;pwd=123456";

        /// <summary>
        /// 测试数据库连接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTestDBConnect_Click(object sender, EventArgs e)
        {
            bool res =false;//= MySqlHelper.TestConnect(this.txt_IP.Text.Trim(), this.txt_UserName.Text.Trim(), this.txt_Pwd.Text.Trim(), this.txt_DBName.Text.Trim());

            string con = "Data Source=.;Initial Catalog=TestDB;Persist Security Info=True;User ID=sa;pwd=123456";

            res = SqlHelper.TestConnect(con);

            if (res)
            {
                MessageBox.Show("数据库连接成功！");
            }
            else
            {
                MessageBox.Show("数据库连接失败！");
            }
        }

        #endregion
        
        private void btn_SelectTables_Click_1(object sender, EventArgs e)
        {
            this.dgv_ShowTables.DataSource = SqlHelper.ExecuteDataSet("select * from sysobjects where xtype='U';", "tables", null).Tables[0];
        }

        /// <summary>
        /// 查询表字段详细数据
        /// </summary>
        /// <param name="tableName"></param>
        public void SelectColumns(string tableName)
        {
            //is_identity 自动增长列
            //case when a.is_nullable = 0 then 'Not Null' else 'Null' end as nullable,
            string cmdSql = @"select 
                            a.name columnname,c.name as typename, 
                            a.is_nullable,
                            a.max_length,a.is_identity

                            from sys.columns a , sys.objects b, sys.types c
                            where a.object_id = b.object_id and b.name='{0}' and c.name <> 'sysname'
                            and a.system_type_id=c.system_type_id order by a.column_id";

            this.dgv_Columns.DataSource = SqlHelper.ExecuteDataSet(String.Format(cmdSql,tableName), "columns", null).Tables[0];

            this.tsLable_CurTable.Text = String.Format("当前表名称：{0}", tableName);
        }

        /// <summary>
        /// 双击查看表字段详细数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgv_ShowTables_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            string tableName = this.dgv_ShowTables.Rows[e.RowIndex].Cells["name"].Value.ToString();
            SelectColumns(tableName);
        }
    }
}
